//
//  MenuLayer.cpp
//  iFruitGame
//
//  Created by sxz on 2014/11/16.
//
//

#include <iostream>
#include "MenuLayer.h"
#include "GameLayer.h"
#include "Demo.h"

#include "Comm/Macro.h"

//SimpleAudioEngineが利用できるように設定する
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

Scene* MenuLayer::createScene()
{
    auto scene = Scene::create();
    
    auto layer = MenuLayer::create();
    
    scene->addChild(layer);
    
    std::cout << "this is a demo from cpp" << std::endl;
    
    return scene;
}

bool MenuLayer::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Demo::demoProint();
    
    //Demo.demoProint();
    
    // [0] play the music
    //あらかじめ、音楽データを読み込む
    SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(1.0);
    SimpleAudioEngine::getInstance()->preloadBackgroundMusic("startmusic.mp3");
    
    //音楽を再生する
    SimpleAudioEngine::getInstance()->playBackgroundMusic("startmusic.mp3");
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    // [1] add the background to the screen center
    auto background = Sprite::create("start_bg@2x.jpg");
    auto spriteSize = background->getContentSize();
    background->setScale(1, 1);

    background->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 + 88));
    this->addChild(background, 0);
    
    // [2] add the logo to the screen
    auto logo = Sprite::create("logo.png");
    logo->setPosition(Vec2(visibleSize.width / 2, 568));
    this->addChild(logo);
    logo->setAnchorPoint(Vec2(0.5, 0));

    // [3] add the start button to the screen
    auto startItem = MenuItemImage::create("play_button@2x.png", "play_button_click@2x.png", CC_CALLBACK_1(MenuLayer::menuCloseCallback, this));
    startItem->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + startItem->getContentSize().height/2 + 318));
    startItem->setScale(1.2);
    
    // create menu, it's an autorelease object
    auto menu = Menu::create(startItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    
    return true;
}

void MenuLayer::onNewGame()
{
    
}

void MenuLayer::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
    //Director::getInstance()->replaceScene(GameLayer::createScene());
    Director::getInstance()->replaceScene(TransitionFadeTR::create(1.5f, GameLayer::createScene()));
    
}
